/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.UI;

import java.util.ArrayList;
import java.util.HashMap;
import javaapplication6.ControlLayer.OrderSellControl;
import javaapplication6.DomainLayer.Customer;
import javaapplication6.DomainLayer.OrderSellProduct;
import javaapplication6.DomainLayer.SellProduct;


/**
 *
 * @author meydlo
 */
public class OrderSellUI {

    private long customerId;
    private boolean enoughGoodsInStok;

    CustomerUI customerUI=new CustomerUI();
    private SellUI sellUI;
    private OrderSellControl orderSellControl;
    private Listener listener;


    public OrderSellUI(){
        sellUI=new SellUI();
        orderSellControl=new OrderSellControl();
        listener=new Listener();
        enoughGoodsInStok=false;
    }
    public void printChoices()
    {
        System.out.println(
                "\n (1) Create Order"+
                "\n (2) confirm order"+
                "\n (3) print orders"+
                "\n (4) main menu"
                );
    }
    public void orderOrderSellProductManager()
    {
        boolean exit=false;
        while(exit==false){
            printChoices();
            int number=listener.getLineInt();
            switch(number){
                case 1:{
                    processSale();
                    break;
                }
                case 2:{
                    confirmOrder();
                    break;
                }
                case 3:{
                    PrintOrdersHashMap();
                    break;
                }
                case 4:{
                    exit=true;
                    break;
                }
                case -1:{
                    System.out.println("Please write a number");
                    break;
                }
                default:{
                    System.out.println("Valid rank id: 1-4");
                }
            }
        }
    }

    //iniciate whole process of sale
    public void processSale()
    {
        Customer customer=customerUI.getCustomerByIDui();
        if(customer!=null){
            customerId=customer.getId();
            ArrayList<OrderSellProduct> orderSellBasketList = new ArrayList<OrderSellProduct>();
            createOrderSell1(customerId,orderSellBasketList);
        }
    }

    //we want to put copy of goods to basket and make order
    public void createOrderSell1(long customerId, ArrayList<OrderSellProduct> orderSellProductList)
    {

        //C choosing product
        SellProduct sellProduct=sellUI.getSellProductByIDui();
         
        long productId=0;

        //if product E
        if(sellProduct!=null){

            productId=sellProduct.getId();
            
            //how much we need?
            int noOfType=getNoOfTypeUI();

            //check if are enough goods in stok to sale
            enoughGoodsInStok=orderSellControl.enoughGoodsInStok(noOfType, sellProduct);

            if(enoughGoodsInStok){

                OrderSellProduct orderSellProduct;

                //making copy of product which we need and how much we need
                orderSellProduct=orderSellControl.setOrderSellProduct(sellProduct,noOfType,customerId);

                //print info about copy of product
                orderSellProduct.printProductInfo();

                System.out.println("Put this good/s to basket?(y/n): ");
                boolean putToBasket=listener.getLineBoolean();

                if(putToBasket==true){

                    //puttting copy of goods to basket
                    orderSellProductList.add(orderSellProduct);

                    //remove productds from stok (decrease nuoOfOneType)
                    orderSellControl.removeProductFromProductList(productId,noOfType);

                    //iniciate next part of shopping process
                    continueInShopping(orderSellProductList,customerId);

                }
                else{
                    //iniciate next part of shopping process
                    continueInShopping(orderSellProductList,customerId);
                }
            }
            else{

                System.out.println("Not enough goods in stok"+
                        "\nin stok: "+sellProduct.getNoOfOneType()+"\n"
                        );

                //starting next part of shopping process
                continueInShopping(orderSellProductList,customerId);
            }
        }
    }


    public void continueInShopping(ArrayList<OrderSellProduct> orderSellProductList, long customerId)
    {
        System.out.println("continue in shopping? (y/n):");
        boolean continueInShopping=listener.getLineBoolean();
        boolean emptyBasket=orderSellProductList.isEmpty();
        
        //want continue
        if(continueInShopping==true){

            //continue in shopping
            createOrderSell1(customerId, orderSellProductList);
        }
        else if(!emptyBasket){

            //print whole basket
            orderSellControl.printBasket(orderSellProductList);

            //option to make order
            System.out.println("Make order? (y/n): ");
            boolean makeOrder=listener.getLineBoolean();

            //handle confirmation (if confirm=true store to HashMap else clear Basket
            orderSellControl.confirmOrder(orderSellProductList, makeOrder);

        }
        else{
            System.out.println("Ordering sucessfully canceled.");
        }
    }

    public void confirmOrder()
    {
        //declare orderId
        long orderId=setOrderId();
        
        if(orderSellControl.orderExist(orderId)){
            System.out.println("Want to confirm?(y) or cancel?(n)");
            boolean isConfirmed=listener.getLineBoolean();

            //handle confirm ordering (if confirmed:( move orderHashMap to LoghashMap) else: (remove from orderHashMap)
            orderSellControl.confirmToBuy(orderId, isConfirmed);
        }
        else{
            System.out.println("Order doesn't exist");
        }
    }

    public long setOrderId()
    {
        System.out.println("Write OrderId: ");
        long orderId=listener.getLineInt();

        return orderId;
    }

    public long setProductId()
    {
        System.out.println("Write id of Product: ");
        long productId=listener.getLineInt();
        return productId;
    }

    public int getNoOfTypeUI()
    {
        System.out.println("Write amount of this type: ");
        int noOfType=listener.getLineInt();
        return noOfType;
    }

    public void PrintOrdersHashMap()
    {
        System.out.println("print it in detail? (y/n): ");
        boolean detail=listener.getLineBoolean();
        HashMap<Long, ArrayList<OrderSellProduct>> orderSellHashMap=orderSellControl.getOrderSellHashMapC();
        orderSellControl.printOrdersHashMap(orderSellHashMap,detail);
    }

}
